package org.iteam.core.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.iteam.core.Constant;
import org.iteam.core.view.TreeNode;

public abstract class CenterService extends BaseService<Object> {
	private static CenterService serviceMysql = null;
	private static CenterService serviceOracle = null;

	public static CenterService getInstance() {
		Constant.DataBaseType dbTypes = Constant.dataBaseType;
		if (dbTypes.equals(Constant.DataBaseType.MYSQL)) {
			return getMySqlInstance();
		} else if (dbTypes.equals(Constant.DataBaseType.ORACLE)) {
			return getOracleInstance();
		}
		return null;
	}

	public static CenterService getMySqlInstance() {
		if (serviceMysql == null)
			serviceMysql = new org.iteam.core.service.mysql.impl.CenterServiceImpl();
		return serviceMysql;
	}

	public static CenterService getOracleInstance() {
		if (serviceOracle == null)
			serviceOracle = new org.iteam.core.service.oracle.impl.CenterServiceImpl();
		return serviceOracle;
	}

	public abstract List<TreeNode> getMenusByRole(String role)
			throws SQLException;
	/**
	 * 排序
	 * @param list
	 * @return
	 */
	protected List<TreeNode> sort(List<TreeNode> list) {
		Object[] nodes = list.toArray();
		int n = nodes.length;
		for (int i = 0; i < n; i++) {
			for (int j = i; j < n; j++) {
				if (((TreeNode) nodes[i]).getSort() < ((TreeNode) nodes[j])
						.getSort())// 比较交换相邻元素
				{
					Object temp  = nodes[i];
					nodes[i] = nodes[j ];
					nodes[j ] = temp;
				}
			}
		}
		List<TreeNode> ls = new ArrayList<TreeNode>();
		for (Object treeNode : nodes) {
			ls.add((TreeNode) treeNode);
		}
		return ls;
	}

}
